//
//  CBMediator.h
//  LZMH
//
//  Created by 肖忠肯 on 16/8/9.
//  Copyright © 2016年 xmlzmh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>

#define OPEN_DOOR_REQUEST_TIME 5.0
#define OPEN_DOOR_BLE_REQ_TIME 3.5


@protocol CBMediatorDelegate <NSObject>
@optional

/**
 手机蓝牙状态变化回调

 @param state 蓝牙状态
 */
- (void)CBMediatorUpdateState:(CBManagerState)state;

/**
 蓝牙从设备搜索结果

 @param p 搜到的目标蓝牙从设备
 @param advertisementData 蓝牙从设备广播信息
 @param RSSI 蓝牙从设备信号强度
 */
- (void)didDiscoverPeripheral:(CBPeripheral *)p advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI;

/**
 搜索蓝牙超时
 */
- (void)searchPeripheralTimesup;

@end


@interface CBMediator : NSObject

@property (nonatomic, weak) id<CBMediatorDelegate> delegate;
@property (readonly, nonatomic) CBCentralManagerState bluetoothState;

+ (instancetype)sharedCBMediator;

/**
 蓝牙开门
 
 @param mac 设备蓝牙MAC地址
 @param command 蓝牙开门指令
 @param second 用户自定义蓝牙开门超时时间 3s左右为宜
 @param completion 蓝牙开门结果回调 1成功 2失败 3超时
 */
- (void)openDoorByBLE:(NSString *)mac withCommand:(NSString *)command requstTimeout:(NSTimeInterval)second completion:(void(^)(NSInteger result))completion;


/**
 搜索附近目标蓝牙设备

 @param timeout 搜索超时时间
 */
- (void)scanPeripheralsWithTimeout:(NSTimeInterval)timeout;

/**
 停止搜索
 */
- (void)stopScanPeripherals;

/**
 手机蓝牙是否开启
 */
- (BOOL)isBluetoothOn;

@end
